package org.lmw.tools.kd;

import org.lmw.tools.kd.adapter.HistoryListAdapter;
import org.lmw.tools.kd.conn.ConnHelper;

import android.app.AlertDialog;
import android.app.Service;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Vibrator;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import android.widget.TextView;

public class History extends BaseActivity implements OnItemClickListener,OnItemLongClickListener{
	private TextView count;
	private ListView lv;
	private HistoryListAdapter adapter;
	private Handler hd;
	private Vibrator mVibrator=null; // 声明一个振动器对象
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_history);
		initView();
	}

	public void initView(){
			adapter=new HistoryListAdapter(this, APP.logs);
			lv=(ListView)findViewById(R.id.historyListView);
			lv.setAdapter(adapter);
			
			lv.setOnItemLongClickListener(this);
			lv.setOnItemClickListener(this);
			count=(TextView)findViewById(R.id.count);
			count.setText(""+APP.logs.size());
			hd=getHandler();
	}
	private Handler getHandler(){
		return new Handler(){
		@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				if(msg.what==0){
					Intent it=new Intent(History.this, Result.class);
					it.putExtra("jsonStr", msg.obj.toString());
					startActivity(it);
					overridePendingTransition(R.anim.push_up_in, R.anim.push_up_out);
				}
				if(msg.what==-1){
					showToast("error");
				}
				dismissLoadingDialog();
			}	
		};
	}
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		showLoadingDialog("");
		ConnHelper.Serach(APP.logs.get(arg2).getComId(),APP.logs.get(arg2).getExpressId(), hd);
	}

//	//长按删除
	@Override
	public boolean onItemLongClick(AdapterView<?> arg0, View arg1,final int arg2,long arg3) {
		mVibrator = (Vibrator) getApplication().getSystemService(Service.VIBRATOR_SERVICE);
		mVibrator.vibrate(new long[] { 50, 50, 0, 0 }, -1);
		AlertDialog.Builder builder = new AlertDialog.Builder(History.this);
		builder.setTitle("确认删除");
		builder.setPositiveButton("确    认",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
					APP.removeLog(APP.logs.get(arg2));
					adapter.notifyDataSetChanged();
					count.setText(""+APP.logs.size());
					}
				});
				builder.setNegativeButton("取    消",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
				
		builder.show();
		return true;
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		overridePendingTransition(R.anim.push_down_in, R.anim.push_down_out);
	}

} 
